<template>
  <el-dialog width="400" :title="dialogTitle" v-model="dialogVisible" modal @close="closeDialog" draggable
    :close-on-click-modal="false" :append-to-body="true">
    <el-form class="dialog-form" :model="formData" ref="vForm" label-position="right" label-width="100px" size="medium"
      @submit.native.prevent :rules="formRules">
      <el-form-item label="UID" prop="uid">
        <el-input v-model="formData.uid" placeholder="请输入UID" link clearable />
      </el-form-item>
      <el-form-item label="通道门名称" prop="deviceName">
        <el-input v-model="formData.deviceName" placeholder="请输入通道门名称" link maxlength="90" show-word-limit clearable />
      </el-form-item>
      <el-form-item label="设备IP" prop="deviceIp">
        <el-input v-model="formData.deviceIp" placeholder="请输入设备IP" link clearable />
      </el-form-item>
      <el-form-item label="端口" prop="devicePort">
        <el-input v-model.number="formData.devicePort"
          onkeyup="value=value.replace(/^\D*(\d*(?:\.\d{0,2})?).*$/g, '$1')" placeholder="请输入端口" clearable
          maxlength="5">
        </el-input>
      </el-form-item>
      <el-form-item label="备注" prop="note">
        <el-input v-model="formData.note" placeholder="请输入备注" type="textarea" :rows="2" maxlength="255" show-word-limit
          clearable />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="closeDialog" size="mini">取 消</el-button>
      <el-button type="primary" @click="submitForm" size="mini">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { add, update, getInfo } from '@/api/bas/rfidDoor'
export default {
  name: 'doorDialog',
  data() {
    return {
      type: '',
      dialogTitle: '',
      dialogVisible: false,
      formData: {},
      formDisabled: false,
      formRules: {
        uid: [{ required: true, message: '请输入通道门uid', trigger: 'blur' }],
        deviceName: [{ required: true, message: '请输入通道门名称', trigger: 'blur' }],
        deviceIp: [
          { required: true, message: '请输入设备IP', trigger: 'blur' },
          { validator: this.validDeviceIp, trigger: 'blur' },
        ],
        devicePort: [{ required: true, message: '请输入端口', trigger: 'blur' }],
      },
    }
  },
  created() { },
  methods: {
    validDeviceIp(rule, value, callback) {
      const reg =
        /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/
      if (!value) {
        return callback()
      }
      setTimeout(() => {
        if (reg.test(value)) {
          callback()
        } else {
          return callback(new Error('输入格式不合法！'))
        }
      })
    },
    add() {
      this.formDisabled = false
      this.type = 'add'
      this.dialogTitle = '新增通道门'
      // 先清空表单数据
      this.formData = {
        devicePort: '2000',
      }
      this.dialogVisible = true
    },
    edit(row) {
      this.formDisabled = true
      this.type = 'edit'
      this.dialogTitle = '编辑通道门'
      getInfo(row.deviceId)
        .then((res) => {
          if (res.data.code === 200) {
            this.formData = res.data.data
            this.dialogVisible = true
          } else {
            this.$message.error(res.data.message)
          }
        })
        .catch((err) => {
          this.$message.error(err)
        })
    },
    submitForm() {
      this.$refs['vForm'].validate((valid) => {
        if (valid) {
          if (this.type === 'add') {
            add(this.formData)
              .then((res) => {
                if (res.data.code === 200) {
                  this.$message.success('新增成功')
                  this.closeDialog()
                  this.$emit('ok')
                } else {
                  this.$message.warning(res.data.message)
                }
              })
              .catch((err) => {
                this.$message.error(err)
              })
          } else {
            update(this.formData)
              .then((res) => {
                if (res.data.code === 200) {
                  this.$message.success('编辑成功')
                  this.closeDialog()
                  this.$emit('ok')
                } else {
                  this.$message.warning(res.data.message)
                }
              })
              .catch((err) => {
                this.$message.error(err)
              })
          }
        }
      })
    },
    closeDialog() {
      this.$refs['vForm'].resetFields()
      this.dialogVisible = false
    },
  },
}
</script>

<style scoped>
.dialog-form {
  padding: 0 30px;
  /* 根据需求调整数值 */
}
</style>
